﻿#include "signupui.h"
#include "ui_signupui.h"

signUpUI::signUpUI(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::signUpUI)
{
    ui->setupUi(this);

    this->setFixedSize(this->size());//锁定窗口大小
}

signUpUI::~signUpUI()
{
    delete ui;
}

void signUpUI::on_btnCancle_clicked()
{
    this->reject();
}


void signUpUI::on_btnOk_clicked()
{
    if(ui->editAccount->text().isEmpty() || ui->editPswd->text().isEmpty() || ui->editRePswd->text().isEmpty()){//如果账号密码没有输入
        QMessageBox::warning(0,"Warning","Please enter complete information.");
        return;
    }

    if(ui->editPswd->text() != ui->editRePswd->text()){//如果两次密码输入不一致
        QMessageBox::warning(0,"Warning","Two passwords do not match..");
        return;
    }

    QString account = ui->editAccount->text();//账号
    QString pswd = ui->editPswd->text();//密码
    int role;//角色类型
    if(ui->combType->currentText() == "Sick"){
        role = 2;
    }else if(ui->combType->currentText() == "Doctor"){
        role = 1;
    }
    QSqlQuery query;
    query.exec("select * from tb_sick where account='"+account+"';");//在客户表中检查是否存在该账号
    if(query.size() > 0){
        QMessageBox::warning(0,"Warning","The account already exists.");
        return;
    }
    query.exec("select * from tb_doctor where account='"+account+"';");//在医生表中检查是否存在该账号
    if(query.size() > 0){
        QMessageBox::warning(0,"Warning","The account already exists.");
        return;
    }
    query.exec("select * from tb_admin where account='"+account+"';");//在管理员表中检查是否存在该账号
    if(query.size() > 0){
        QMessageBox::warning(0,"Warning","The account already exists.");
        return;
    }

    //如果三个登陆信息记录表中都不存在该账号则可以注册
    QString age = ui->spinAge->text();
    QString sex = ui->combSex->currentText();
    QString phone = ui->editPhone->text();
    QString address = ui->editAddress->text();

    if(role == 2){//注册客户
        if(!query.exec("insert into tb_sick(account,pswd,age,sex,phone,address) values('"+account+"','"+pswd+"',"+age+",'"+sex+"','"+phone+"','"+address+"');")){
            QMessageBox::warning(0,"Warning","Fail to register!");
            return;
        }
    }else if(role == 1){//注册医生
        if(!query.exec("insert into tb_doctor(account,pswd,age,sex,phone,address) values('"+account+"','"+pswd+"',"+age+",'"+sex+"','"+phone+"','"+address+"');")){
            QMessageBox::warning(0,"Warning","Fail to register!");
            return;
        }
    }

    QMessageBox::information(0,"Information","Registered successfully!");
    this->accept();
}

